#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <assert.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/dir.h>
#include <sys/stat.h>

#include "bstrlib.h"
#include "server.h"
#include "common.h"
#include "receive.h"
#include "send_file_listing.h"
#include "send_metadata.h"
#include "send_slice.h"
int send_msg(int fd_client, char *msg){
    /*Esta funcion la utilizamos para las respuestas desde al cliente*/
    int aux_send = 0, length = 0, i=0, finished=0;
    char *aux_msg = NULL;
    assert (msg != NULL);
    assert (fd_client != 0);

    length = strlen(msg);

    while(finished == 0){
        aux_send = send(fd_client, msg, length, 0);
        if (aux_send == -1){
            perror("send");
            if (errno==ECONNRESET || errno==EPIPE || errno==ESPIPE){
                return -1;
            }
            return 0;
        }
        if (aux_send == length){
                finished = 1;
        }
        else{
            aux_msg = calloc(length,sizeof(char*));
            for (i=aux_send; i<length; i++){
                aux_msg[i-aux_send]=msg[i];
            }
            strncpy(msg, aux_msg, aux_send);
            length = strlen(msg);
            free(aux_msg);
            aux_msg = NULL;
        }
    }
    return 0;
}
